package com.twetec.cms.service.website.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.dao.mapper.website.ColumnMapper;
import com.twetec.cms.dao.mapper.website.SiteMapper;
import com.twetec.cms.model.po.website.ColumnPO;
import com.twetec.cms.model.po.website.SiteColumnPO;
import com.twetec.cms.dao.mapper.website.SiteColumnMapper;
import com.twetec.cms.model.vo.website.SiteColumnTempVO;
import com.twetec.cms.model.vo.website.SiteColumnVO;
import com.twetec.cms.service.website.SiteColumnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2021-09-06
 */
@Service
public class SiteColumnServiceImpl extends ServiceImpl<SiteColumnMapper, SiteColumnPO> implements SiteColumnService {

    @Autowired
    private SiteColumnMapper siteColumnMapper;

    @Autowired
    private ColumnMapper columnMapper;

    /**
     * 在创建栏目的时候调用该方法。
     *
     * @param columnID 栏目ID
     * @param siteID   站点ID
     * @return
     */
    @Override
    public boolean create(Long columnID, Long siteID) {
        SiteColumnPO siteColumnPO = new SiteColumnPO();
        siteColumnPO.setSiteId(siteID);
        siteColumnPO.setColumnId(columnID);
        int insert = siteColumnMapper.insert(siteColumnPO);
        return insert > 0;
    }

    /**
     * 查询站点ID对应的顶级栏目
     *
     * @param siteID
     * @return
     */
    @Override
    public List<SiteColumnPO> listBySiteID(Long siteID) {
        QueryWrapper<SiteColumnPO> queryWrapper = new QueryWrapper<>();
        // Where
        queryWrapper.eq("SITE_ID", siteID);

        List<SiteColumnPO> siteColumnPOList = siteColumnMapper.selectList(queryWrapper);

        return siteColumnPOList;
    }


    /**
     * 查询站点下为未被回收的的所有栏目id
     * @param siteId
     * @return
     */
    @Override
    public List<Long> getNotRecoveryColumnIdBySiteId(Long siteId) {

        ArrayList<Long> idList = new ArrayList<>();

        // 站点id

        // 中间表中查询站点对应的顶级栏目
        QueryWrapper<SiteColumnPO> siteColumnWrapper = new QueryWrapper<>();
        siteColumnWrapper.eq("SITE_ID", siteId);
        List<SiteColumnPO> siteColumnList = siteColumnMapper.selectList(siteColumnWrapper);
        // 如果没有查到,返回一个空集合
        if (ValidateUtil.isNullOrEmpty(siteColumnList)) {
            return null;
        }
        // 获取顶级栏目的id
        ArrayList<Long> columnIdList = new ArrayList<>();
        for (SiteColumnPO siteColumnPO : siteColumnList) {
            columnIdList.add(siteColumnPO.getColumnId());
        }

        // 再通过集合到栏目表中去查询，以筛选出没有被回收的栏目id，并装到id的集合中去
        QueryWrapper<ColumnPO> columnTempWrapper = new QueryWrapper<>();
        columnTempWrapper.in("ID", columnIdList);
        columnTempWrapper.eq("IS_RECOVERY", 0);
        List<ColumnPO> columnPOList = columnMapper.selectList(columnTempWrapper);

        // 如果没有未被回收的顶级栏目，返回null
        if (ValidateUtil.isNullOrEmpty(columnPOList)) {
            return null;
        }

        // 如果有未被回收的顶级栏目,添加到栏目的集合中去
        for (ColumnPO columnPO : columnPOList) {
            idList.add(columnPO.getId());
        }

        // 查询未被回收的子栏目
        ArrayList<String> xPathList = new ArrayList<>();
        for (Long columnId : idList) {
            String xPath = "0-" + columnId;
            xPathList.add(xPath);
        }
        QueryWrapper<ColumnPO> queryWrapper = new QueryWrapper<>();
        // 未被回收
        queryWrapper.eq("IS_RECOVERY", 0);
        for (int i = 0; i < xPathList.size(); i++) {
            // 到了最后一个，或只有一个时
            if (i == (xPathList.size() - 1)) {
                queryWrapper.likeRight("XPATH", xPathList.get(i));
            } else {
                queryWrapper.likeRight("XPATH", xPathList.get(i)).or();
            }
        }
        // 站点下所有未被回收的子级栏目
        List<ColumnPO> childColumnPOList = columnMapper.selectList(queryWrapper);

        // 如果没有未被回收的子栏目
        if (ValidateUtil.isNullOrEmpty(childColumnPOList)) {
            return idList;
        }
        // 添加子栏目id
        for (ColumnPO columnPO : childColumnPOList) {
            idList.add(columnPO.getId());
        }

        return idList;
    }


    /**
     * 查询站点下被回收的所有栏目id
     * @param siteId
     * @return
     */
    public List<Long> getRecoveryColumnIdBySiteId(Long siteId) {
        ArrayList<Long> idList = new ArrayList<>();

        // 站点id

        // 中间表中查询站点对应的顶级栏目
        QueryWrapper<SiteColumnPO> siteColumnWrapper = new QueryWrapper<>();
        siteColumnWrapper.eq("SITE_ID", siteId);
        List<SiteColumnPO> siteColumnList = siteColumnMapper.selectList(siteColumnWrapper);
        // 如果没有查到,返回一个空集合
        if (ValidateUtil.isNullOrEmpty(siteColumnList)) {
            return null;
        }
        // 获取顶级栏目的id
        ArrayList<Long> columnIdList = new ArrayList<>();
        for (SiteColumnPO siteColumnPO : siteColumnList) {
            columnIdList.add(siteColumnPO.getColumnId());
        }

        // 再通过集合到栏目表中去查询，以筛选出被回收的栏目id，并装到id的集合中去
        QueryWrapper<ColumnPO> columnTempWrapper = new QueryWrapper<>();
        columnTempWrapper.in("ID", columnIdList);
        columnTempWrapper.eq("IS_RECOVERY", 1);
        List<ColumnPO> columnPOList = columnMapper.selectList(columnTempWrapper);

        // 如果没有被回收的顶级栏目，返回null
        if (ValidateUtil.isNullOrEmpty(columnPOList)) {
            return null;
        }

        // 如果有被回收的顶级栏目,添加到栏目的集合中去
        for (ColumnPO columnPO : columnPOList) {
            idList.add(columnPO.getId());
        }

        // 被回收的顶级栏目下的所有栏目id
        ArrayList<String> recoveryChild = new ArrayList<>();
        for (Long columnId : idList) {
            String xPath = "0-" + columnId;
            recoveryChild.add(xPath);
        }
        QueryWrapper<ColumnPO> queryRecoveryWrapper = new QueryWrapper<>();
        // 拼接xpath
        for (int i = 0; i < recoveryChild.size(); i++) {
            // 到了最后一个，或只有一个时
            if (i == (recoveryChild.size() - 1)) {
                queryRecoveryWrapper.likeRight("XPATH", recoveryChild.get(i));
            } else {
                queryRecoveryWrapper.likeRight("XPATH", recoveryChild.get(i)).or();
            }
        }
        // 被回收的顶级栏目下，所有的子级栏目
        List<ColumnPO> childRecoveryColumnPOList = columnMapper.selectList(queryRecoveryWrapper);
        for (ColumnPO columnPO : childRecoveryColumnPOList) {
            idList.add(columnPO.getId());
        }

        // 未被回收的顶级栏目id
        ArrayList<Long> noRecoveryIdList = new ArrayList<>();
        for (ColumnPO columnPO : columnPOList) {
            noRecoveryIdList.add(columnPO.getId());
        }


        // 从未被回收的顶级栏目的子栏目中，查询被回收的子栏目
        ArrayList<String> xPathList = new ArrayList<>();
        for (Long columnId : noRecoveryIdList) {
            String xPath = "0-" + columnId;
            xPathList.add(xPath);
        }
        QueryWrapper<ColumnPO> queryWrapper = new QueryWrapper<>();
        // 拼接xpath
        queryWrapper.eq("IS_RECOVERY", 1);
        for (int i = 0; i < xPathList.size(); i++) {
            // 到了最后一个，或只有一个时
            if (i == (xPathList.size() - 1)) {
                queryWrapper.likeRight("XPATH", xPathList.get(i));
            } else {
                queryWrapper.likeRight("XPATH", xPathList.get(i)).or();
            }
        }
        // 未被回收的顶级栏目下，被回收的子级栏目
        List<ColumnPO> childColumnPOList = columnMapper.selectList(queryWrapper);

        // 如果没有被回收的子栏目
        if (ValidateUtil.isNullOrEmpty(childColumnPOList)) {
            return idList;
        }
        // 有就添加到回收栏目id集合中
        for (ColumnPO columnPO : childColumnPOList) {
            idList.add(columnPO.getId());
        }

        return idList;
    }
}
